<?php
$this->frontend_lib->setACss(array(assets('css/fotorama.css', true)));
$this->frontend_lib->setAJs(array(assets('js/fotorama.js', true), assets('js/front.js', true)));

$startIndex = 0;
$selected_id = 0;
$selected_code = '';
$query_code = $this->input->get('code', true);
if($query_code!=false && trim($query_code)!=''){
	$selected_code = $query_code;
	foreach($productDetails as $index=>$item){
		if($item->getCode() == $selected_code){
			$startIndex = $index;
			$selected_id = $item->getId();
			break;
		}
	}
}
if($startIndex==0 && $selected_id==0) {
	foreach($productDetails as $index=>$item){
		if($item->getIsAvatar()==1){
			$startIndex = $index;
			$selected_id = $item->getId();
			$selected_code = $item->getCode();
			break;
		}
	}
}
?>

<div class="row">
	<div class="col-md-12 col-xs-12 col-sm-12 mgt-10">
		<ol class="breadcrumb">
			<li><a class="text-uppercase" href="<?php echo shop_url($this->shop->getSlug()); ?>">Trang chủ</a></li>
			<li><a class="text-uppercase" href="<?php echo shop_url($this->shop->getSlug().'/'.CATEGORY_SLUG.'/'.$product->shop_cate_slug); ?>"><?php echo $product->shop_cate_title; ?></a></li>
			<li class="active"><span class="text-uppercase"><?php echo $product->getTitle(); ?></span></li>
		</ol>
	</div>

	<div class="col-md-7 col-xs-12 col-sm-12">
		<div class="row">
			<div class="col-md-6 col-xs-12 col-sm-12">
				<div class="fotorama" data-start-index='<?php echo $startIndex; ?>' id="product-img-slide">
					<?php
					$product_price_new = '';
					$div_desc          = '';
					foreach($productDetails as $item) {
						if($product_price_new == '') {
							if($selected_code!=''){
								if($item->getCode() == $selected_code) {
									$product_price_new = $item->getPriceNew();
								}
							} else {
								if($item->getIsAvatar()==1) {
									$product_price_new = $item->getPriceNew();
								}
							}
						}
					?>
					<a href="<?php echo product_upload($item->image, $product->shop_image_folder, false); ?>">
						<img src="<?php echo product_upload($item->image, $product->shop_image_folder); ?>" data-caption='<?php echo $item->getCode(); ?>'/>
					</a>
					<?php } ?>
				</div>
				<div class="social-bar-h mgt-10">
					<div class="fb-like" data-href="<?php echo product_url($product->getSlug()); ?>" data-layout="box_count" data-action="like" data-show-faces="true"></div>
					<div class="g-plusone" data-href="<?php echo product_url($product->getSlug()); ?>" data-size="tall"></div>
				</div>
			</div>

			<div class="col-md-6 col-xs-12 col-sm-12 single-product-info">
				<form has-validate id="frm-product-detail" method="post" action="<?php echo page_url('cart/direct-push'); ?>">
		            <input type="hidden" id="product-id" name="cart[id]" value="<?php echo $selected_id; ?>" />
		            <input type="hidden" id="product-price" name="cart[price]" value="<?php echo $product_price_new; ?>" />
	            	<h2 class="text-uppercase"><?php echo $product->getTitle(); ?></h2>
					<div class="available">
						<p>Sản phẩm cùng loại:</p>
						<select id="product-temp-detail" class="form-control w100">
						<?php						
						foreach($productDetails as $index=>$item) {
							$selected_option = '';
							if($selected_option==''){
								if($selected_code!=''){
									if($item->getCode() == $selected_code) {
										$selected_option = "selected='selected'";
									} else {
										if($item->getIsAvatar()==1) {
											$selected_option = "selected='selected'";
										}
									}
								}
							}
							?>
							<?php

							if($selected_option!=''){
								$div_desc .= "<div class='text-hyphens product-description description-{$item->getId()}'>{$item->getDescription()}</div>";
							} else {
								$div_desc .= "<div class='text-hyphens hidden product-description description-{$item->getId()}'>{$item->getDescription()}</div>";
							}

							$option_data = array(
								'value'                 => $item->getId(),
								'data-price'            => $item->getPriceNew(),
								'data-price-old'        => $item->getPriceOld(),
								'data-price-format'     => currency_format($item->getPriceNew()),
								'data-price-old-format' => currency_format($item->getPriceOld()),
								'data-index'            => $index,
								'data-link'             => product_url($product->slug)
								);
							$option_string = implode(' ', array_map(function ($v, $k) { return $k . "='" . $v."'"; }, $option_data, array_keys($option_data)));
							?>
							<option <?php echo $option_string; ?> <?php echo $selected_option; ?>><?php echo $item->getCode(); ?></option>
						<?php } ?>
						</select>
					</div>
					<div class="mgt-10"><p>Giá : <span id="order-temp-price" class="price price-new"><?php echo currency_format($product_price_new); ?></span></p></div>
					<div class="mgt-10">
						<p>Số lượng :</p>
						<input autocomplete="off" id="product-qty" name="cart[qty]" type="text" class="form-control w100 text-right" value="1" data-validate="required/numeric" data-validate-message="">
					</div>
					<div id="flg_message" class="alert alert-success hidden mgt-10"></div>
					<div class="mgt-10">
						<button class="btn btn-info btn-fill" role="button"><i class="fa fa-check-circle"></i>&nbsp; Mua ngay</button>
	                    <button data-href="<?php echo page_url('cart/push'); ?>" id="btn-add-to-cart" type="button" class="btn btn-info"><i class="fa fa-shopping-cart"></i> Cho vào giỏ hàng</button>
					</div>
				</form>
			</div>
			
			<div class="col-md-12 col-xs-12 col-sm-12"><h3 class="text-uppercase">Mô tả</h3><?php echo $div_desc; ?></div>
		</div>
	</div>

	<div class="col-md-5 col-xs-12 col-sm-12"><div class="fb-comments" data-width="100%" data-href="<?php echo product_url($product->getSlug()); ?>" data-numposts="5" data-colorscheme="light"></div></div>
</div>